package com.ophiux.cs.infrastructure.repositories.consultBase;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.ophiux.cs.domain.consultBase.model.entity.Doctor;
import com.ophiux.cs.domain.consultBase.model.valueobj.CSDoctorsResult;
import com.ophiux.cs.domain.consultBase.model.valueobj.CSTypeResult;
import com.ophiux.cs.domain.consultBase.model.valueobj.DoctorQueryObj;
import com.ophiux.cs.domain.consultBase.repositories.IDoctorRepository;
import com.ophiux.cs.infrastructure.compoment.basedata.mybatis.DataComponent;
import com.ophiux.cs.infrastructure.mapper.consultBase.DoctorMapper;
import com.ophiux.utils.exception.OphiuxDaoException;
import com.ophiux.utils.exception.OphiuxRepositoryException;

import lombok.extern.slf4j.Slf4j;

@Service
@Slf4j
public class DoctorRepository implements IDoctorRepository {
	@Autowired
	DoctorMapper doctorMapper;
	@Autowired
	DataComponent dataComponent;

	@Override
	public void createDoctor(Doctor doctor) throws OphiuxRepositoryException {
		try {
			//根据姓名证件号判断
			Doctor doctorParam=new Doctor();
			doctorParam.setDOCTOR_NAME(doctor.getDOCTOR_NAME());
			doctorParam.setHOSPITAL_ID(doctor.getHOSPITAL_ID());
			doctorParam.setDEPT_ID(doctor.getDEPT_ID());
			doctorParam.setCERTIFICATE_TYPE(doctor.getCERTIFICATE_TYPE());
			doctorParam.setCERTIFICATE_NO(doctor.getCERTIFICATE_NO());
			List<Doctor> docList=doctorMapper.selectDoctor(doctorParam);
			if(docList==null || docList.size()==0){//不存在，新增
				doctorMapper.addDoctor(doctor);
			}else if(docList.size()==1){//存在，更新
				doctor.setDOCTOR_ID(docList.get(0).getDOCTOR_ID());
				doctorMapper.updateDoctor(doctor);
			}else{
				throw new OphiuxRepositoryException("当前医生已注册");
			}
		} catch (OphiuxDaoException e) {
			e.printStackTrace();
			log.info("DoctorRepository=>createDoctor()异常，",e);
			throw new OphiuxRepositoryException("新增医生异常", e);
		}
	}

	@Override
	public void updateDoctor(Doctor doctor) throws OphiuxRepositoryException {
		try {
			doctorMapper.updateDoctor(doctor);
		} catch (OphiuxDaoException e) {
			e.printStackTrace();
			log.info("DoctorRepository=>updateDoctor()异常，",e);
			throw new OphiuxRepositoryException("更新医生异常", e);
		}
	}

	@Override
	public void deleteDoctor(String doctorId) throws OphiuxRepositoryException {
		try {
			doctorMapper.deleteDoctor(doctorId);
		} catch (OphiuxDaoException e) {
			e.printStackTrace();
			log.info("DoctorRepository=>deleteDoctor()异常，",e);
			throw new OphiuxRepositoryException("删除医生异常", e);
		}
	}

	@Override
	public List<Doctor> selectDoctor() throws OphiuxRepositoryException {
		List<Doctor> docList=null;
		try {
			docList=doctorMapper.selectDoctor(null);
		} catch (OphiuxDaoException e) {
			e.printStackTrace();
			log.info("DoctorRepository=>selectDoctor()异常，",e);
			throw new OphiuxRepositoryException("查询医院异常", e);
		}
		return  docList;
	}



	@Override
	public List<CSDoctorsResult> getCSDoctors(DoctorQueryObj doctor) throws OphiuxRepositoryException {
		List<CSDoctorsResult> doctorList=null;
		try {
			doctorList=doctorMapper.getCSDoctors(doctor);
		} catch (OphiuxDaoException e) {
			e.printStackTrace();
			log.info("DoctorRepository=>getCSDoctors()异常，",e);
			throw new OphiuxRepositoryException("查询医生异常", e);
		}
		return doctorList;
	}
	@Override
	public int getCSDoctorsCount(DoctorQueryObj doctor) throws OphiuxRepositoryException {
		int count=0;
		try {
			count=doctorMapper.getCSDoctorsCount(doctor);
		} catch (OphiuxDaoException e) {
			e.printStackTrace();
			log.info("DoctorRepository=>getCSDoctorsCount()异常，",e);
			throw new OphiuxRepositoryException("查询医生异常", e);
		}
		return count;
	}
	@Override
	public Doctor getDoctorByID(String DOCTOR_ID) throws OphiuxRepositoryException{
		Doctor doctor=null;
		try {
			doctor=doctorMapper.getDoctorById(DOCTOR_ID);
		} catch (OphiuxDaoException e) {
			e.printStackTrace();
			log.info("DoctorRepository=>getDoctorByID()异常，",e);
			throw new OphiuxRepositoryException("更新医生异常", e);
		}
		return doctor;
	}

	@Override
	public List<CSTypeResult> getCSTypeByDoctorId(String DOCTOR_ID)throws OphiuxRepositoryException {
		List<CSTypeResult> typeList=null;
		try {
			typeList=doctorMapper.getCSTypeByDoctorId(DOCTOR_ID);
		} catch (OphiuxDaoException e) {
			e.printStackTrace();
			log.info("DoctorRepository=>getCSTypeByDoctorId()异常，",e);
			throw new OphiuxRepositoryException("更新医生异常", e);
		}
		return typeList;
	}
}
